import { v4 as uuidv4 } from 'uuid';
import html2canvas from 'html2canvas';
import { uploadApi } from '@/api/common.api';
/**
 *
 * @returns uuid
 */
export const uuid = (): string => {
	return uuidv4();
};

/**
 * @docs html转图片上次
 * @param id 元素id
 * @returns
 */
export const takeScreenshotAndUpload = (id: string) => {
	const el = document.getElementById(id) as HTMLElement;
	return html2canvas(el, { allowTaint: false, useCORS: true, width: 375 }).then((canvas: HTMLCanvasElement) => {
		return new Promise(resolve => {
			canvas.toBlob(async blob => {
				if (blob) {
					let formData = new FormData();
					formData.append('file', blob, 'file.jpeg');
					let res: any = await uploadApi(formData);
					resolve(res.urls[0]);
				}
			}, 'image/png');
		});
	});
};
